package com.cytech.cloudclinic.moudle.main.drug;

import com.cytech.cloudclinic.base.BaseBean;
import com.cytech.cloudclinic.base.BaseObserver;
import com.cytech.cloudclinic.base.BasePresenter;
import com.cytech.cloudclinic.bean.Drug;
import com.cytech.cloudclinic.bean.DrugTag;
import com.cytech.cloudclinic.bean.req.GetDrug;
import com.cytech.cloudclinic.bean.resp.RespDrugList;
import com.yechaoa.yutils.ToastUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import okhttp3.MediaType;
import okhttp3.RequestBody;

public class DrugManagerPresenter extends BasePresenter<DrugManagerView> {
    public DrugManagerPresenter(DrugManagerView baseView) {
        super(baseView);
    }

    /**
     * 药品分类列表
     * @param parent_id 父id 为0 查询一级分类
     */
    void getDrugTagList(int parent_id) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("parent_id",parent_id);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String  requestStr = jsonObject.toString();
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"),requestStr);
        addDisposable(apiServer.getDrugTagList(body), new BaseObserver<BaseBean<List<DrugTag>>>(baseView, true) {

            @Override
            public void onSuccess(BaseBean<List<DrugTag>> o) {
                if(o.result!=null){
                    baseView.getDrugChildTagSuccess(o.result);
                }
            }

            @Override
            public void onError(String msg) {
                ToastUtil.showToast(msg);
            }
        });
    }

    /**
     * 药品列表
     */
    void getDrugList(String searchContent,int drug_tag_id) {
        GetDrug req = new GetDrug();
        req.drug_tag_id = drug_tag_id;
        req.s = searchContent;
        addDisposable(apiServer.getDrugList(objectToRequestBody(req)), new BaseObserver<BaseBean<List<Drug>>>(baseView, false) {

            @Override
            public void onSuccess(BaseBean<List<Drug>> o) {
                baseView.getDrugSuccess(o.result);
            }

            @Override
            public void onError(String msg) {
                ToastUtil.showToast(msg);
            }
        });
    }
}
